package com.eqdd.yiqidian.base;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.eqdd.yiqidian.NoticeInfoActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.box.ItemDecoration.SectionDividerItemDecoration;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditApplyTitleBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditHintBean;
import com.eqdd.yiqidian.utils.DensityUtil;
import com.eqdd.yiqidian.utils.ToastUtil;

import java.util.ArrayList;

public abstract class BaseInfoActivity extends BaseActivity {
    protected NoticeInfoActivityCustom dataBinding;
    protected SlimAdapterEx<SlimEditBean> slimAdapterEx;
    protected int id;
    protected ArrayList<SlimEditBean> data = new ArrayList();
    protected int who;
    protected int status;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        isKeyBoardJustPan = false;
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_notice_info);
        initTitle();
    }

    @Override
    public void initData() {
        id = getIntent().getIntExtra(Config.ID, 0);
        who = getIntent().getIntExtra(Config.WHO, 0);
        status = getIntent().getIntExtra(Config.STATUS, 0);
        if (who > 0 && status == 0) {
            initTopRightText("提交", v -> {
                if (checkParam()) {
                    checkPost();
                } else {
                    ToastUtil.showLong("真残忍，能否给个理由先？");
                }
            });
        }
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        dataBinding.recyclerView.addItemDecoration(new SectionDividerItemDecoration(this)
                .setLeftPadding(DensityUtil.percentW(30))
                .setRightPadding(DensityUtil.percentW(40)));
        slimAdapterEx = SlimAdapterEx.create()
                .register(R.layout.list_item_49, new SlimInjector<SlimEditApplyTitleBean>() {
                    @Override
                    public void onInject(SlimEditApplyTitleBean data, IViewInjector injector) {
                        injector.text(R.id.tv_upper_left_content, dealText(data.getTopLeftContent()))
                                .text(R.id.tv_upper_right_content, dealText(data.getTopRightContent()))
                                .text(R.id.tv_down_content, dealText(data.getTitle()));

                    }
                })
                .register(R.layout.list_item_29, new SlimInjector<SlimEditEnterBean>() {
                    @Override
                    public void onInject(SlimEditEnterBean data, IViewInjector injector) {
                        injector.text(R.id.tv_left_content, dealText(data.getTitle()))
                                .text(R.id.tv_right_content, dealText(data.getContent()))
                                .with(R.id.tv_right_content, v -> ((TextView) v).setHint(dealText(data.getHintContent())));
                    }
                })
                .register(R.layout.list_item_28, new SlimInjector<SlimEditHintBean>() {
                    @Override
                    public void onInject(SlimEditHintBean data, IViewInjector injector) {
                        injector.text(R.id.tv_left_content, dealText(data.getTitle()))
                                .text(R.id.tv_right_hint_content, dealText(data.getContent()));
                    }
                }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        dataBinding.rbReject.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                dataBinding.rbAgree.setChecked(!isChecked);
            }
        });
        dataBinding.rbAgree.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                dataBinding.rbReject.setChecked(!isChecked);
            }
        });
        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener((recyclerView, position, v) -> {
                    itemClick(position);
                });
    }

    private boolean checkParam() {
        return !(dataBinding.rbReject.isChecked() && TextUtils.isEmpty(dataBinding.etRemark.getText()));
    }

    protected String dealText(String text) {
        return TextUtils.isEmpty(text) ? " " : text;
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    public abstract void initTitle();

    protected abstract void checkPost();

    protected abstract void itemClick(int position);


}
